package com.qzyh.esb.egsa.utils;

import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 填充工具类
 */
public class FillUtil{

    public static Object fill(Object object) {
        try {
            Method getId = object.getClass().getDeclaredMethod("getId");
            String id = (String)getId.invoke(object);
            if(StringUtils.hasLength(id)){
                //设置修改时间
                Method setUpdateTime = object.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);
                setUpdateTime.invoke(object,LocalDateTime.now());
                //设置修改人员
                Method setUpdateUserId = object.getClass().getDeclaredMethod("setUpdateUserId",String.class);
                setUpdateUserId.invoke(object,BaseContextUtil.getUserId());
            } else {
                //设置创建时间
                Method setCreateTime = object.getClass().getDeclaredMethod("setCreateTime",LocalDateTime.class);
                setCreateTime.invoke(object,LocalDateTime.now());
                //设置创建人员
                Method setCreateUserId = object.getClass().getDeclaredMethod("setCreateUserId",String.class);
                setCreateUserId.invoke(object,BaseContextUtil.getUserId());
                //设置修改时间
                Method setUpdateTime = object.getClass().getDeclaredMethod("setUpdateTime",LocalDateTime.class);
                setUpdateTime.invoke(object,LocalDateTime.now());
                //设置修改人员
                Method setUpdateUserId = object.getClass().getDeclaredMethod("setUpdateUserId",String.class);
                setUpdateUserId.invoke(object,BaseContextUtil.getUserId());
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return object;
    }
}
